package com.coin.exchanges.view.user

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import com.coin.exchanges.R
import com.coin.exchanges.net.RetrofitFactory
import com.coin.exchanges.presenter.TestPresenter
import com.coin.exchanges.utils.LCSharedPreferencesHelper
import com.coin.exchanges.view.user.bean.BindCardBean
import com.coin.libbase.view.activity.JToolbarActivity
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_used_draw.*

class UsedDrawActivity  : JToolbarActivity<TestPresenter>() {
    override fun getLayout(): Int {
        return R.layout.activity_used_draw
    }

    override fun initIntent(intent: Intent?) {
        setToolbarBackground(R.color.white)
        title = "提现"
    }

    override fun initView() {
    }

    override fun initData() {
        mDrawTv.setOnClickListener {
            if(checkBindCard(mPersonDrawCountEt.text.toString(),mPersonDrawNumEt.text.toString())) {
                val sharedPreferencesHelper = LCSharedPreferencesHelper.instance(this@UsedDrawActivity, "register")

                val userId = sharedPreferencesHelper.getValue("UserId")
                PostNetDrawAll(userId,mPersonDrawCountEt.text.toString(),mPersonDrawNumEt.text.toString())
            }
        }
        mDrawIv.setOnClickListener {
            mPersonDrawCountEt.setText("")
        }
        mPersonDrawCardIv.setOnClickListener {
            mPersonDrawNumEt.setText("")
        }
    }

    private fun PostNetDrawAll(user_id: String,money: String,bank_code: String){
        val map = HashMap<String, String>()
        map["user_id"] = user_id
        map["money"] = money
        map["bank_code"] = bank_code
        RetrofitFactory
                .getOkExApiService()
                .draw(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<BindCardBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(t: BindCardBean) {
                        mDrawTip.text = t.returnval
                        //  Toast.makeText(this@UsedChargeActivity, t.returnval, Toast.LENGTH_LONG).show()
                        //finish()
                    }

                    override fun onError(e: Throwable) {
                        Log.e("jignan",e.message)
                    }

                })
    }
    fun checkBindCard(count: String,card: String): Boolean {
        if (TextUtils.isEmpty(count)) {
            Toast.makeText(this, "请输入充值金额", Toast.LENGTH_LONG).show()
            return false
        }
        if (TextUtils.isEmpty(card)) {
            Toast.makeText(this, "请输入银行卡号", Toast.LENGTH_LONG).show()
            return false
        }
        return true}

}